/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.thinkgem.jeesite.modules.order.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.order.entity.QuHuoDan;
import com.thinkgem.jeesite.modules.order.service.QuHuoDanService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 取货单Controller
 * @author 李忠
 * @version 2013-07-28
 */
@Controller
@RequestMapping(value = "${adminPath}/order/quHuoDan")
public class QuHuoDanController extends BaseController {

	@Autowired
	private SystemService systemService;

	@Autowired
	private QuHuoDanService quHuoDanService;

	@ModelAttribute
	public QuHuoDan get(@RequestParam(required = false) Long id) {
		if (id != null) {
			return quHuoDanService.get(id);
		} else {
			return new QuHuoDan();
		}
	}

	@RequiresPermissions("order:quHuoDan:view")
	@RequestMapping(value = { "list", "" })
	public String list(QuHuoDan quHuoDan, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()) {
			quHuoDan.setCreateBy(user);
		}
		Page<QuHuoDan> page = quHuoDanService.find(new Page<QuHuoDan>(request, response), quHuoDan);
		List<User> userList = systemService.findAllUser();
		model.addAttribute("page", page);

		model.addAttribute("userList", userList);

		return "modules/order/quHuoDanList";
	}

	@RequiresPermissions("order:quHuoDan:view")
	@RequestMapping(value = "form")
	public String form(QuHuoDan quHuoDan, Model model) {
		model.addAttribute("quHuoDan", quHuoDan);
		return "modules/order/quHuoDanForm";
	}

	@RequiresPermissions("order:quHuoDan:view")
	@RequestMapping(value = "heduiForm")
	public String heduiForm(QuHuoDan quHuoDan, Model model) {
		model.addAttribute("quHuoDan", quHuoDan);
		return "modules/order/quHuoDanHeDui";
	}

	@RequiresPermissions("order:quHuoDan:edit")
	@RequestMapping(value = "save")
	public String save(QuHuoDan quHuoDan, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, quHuoDan)) {
			return form(quHuoDan, model);
		}
		quHuoDanService.save(quHuoDan);
		addMessage(redirectAttributes, "保存取货单'" + quHuoDan.getNo() + "'成功");
		return "redirect:" + Global.getAdminPath() + "/order/quHuoDan/?repage";
	}

	@RequiresPermissions("order:quHuoDan:edit")
	@RequestMapping(value = "xiadan")
	public String xiadan(QuHuoDan quHuoDan, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, quHuoDan)) {
			return form(quHuoDan, model);
		}
		quHuoDanService.xiadan(quHuoDan);
		addMessage(redirectAttributes, "取货单'" + quHuoDan.getNo() + "'下单成功");
		return "redirect:" + Global.getAdminPath() + "/order/quHuoDan/?repage";
	}

	@RequiresPermissions("order:quHuoDan:edit")
	@RequestMapping(value = "hedui")
	public String hedui(QuHuoDan quHuoDan, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, quHuoDan)) {
			return form(quHuoDan, model);
		}
		quHuoDan.setDelFlag("3");
		quHuoDan.setHdsj(new Date());
		quHuoDanService.save(quHuoDan);
		addMessage(redirectAttributes, "取货单'" + quHuoDan.getNo() + "'核对成功");
		return "redirect:" + Global.getAdminPath() + "/order/quHuoDan/?repage";
	}

	@RequiresPermissions("order:quHuoDan:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		quHuoDanService.delete(id);
		addMessage(redirectAttributes, "删除取货单成功");
		return "redirect:" + Global.getAdminPath() + "/order/quHuoDan/?repage";
	}
	
	@RequiresPermissions("order:quHuoDan:edit")
	@RequestMapping(value = "fenpai")
	public String fenpai(String qhdIds, Long userId, RedirectAttributes redirectAttributes){
		
		quHuoDanService.fenpai(userId, qhdIds);
		addMessage(redirectAttributes, "取货分派单成功");
		return "redirect:" + Global.getAdminPath() + "/order/quHuoDan/?repage";
	}
	
	@RequiresPermissions("order:quHuoDan:edit")
	@RequestMapping(value = "zhengbao")
	public String zhengbao(String qhdIds, RedirectAttributes redirectAttributes){
		
		quHuoDanService.fenpai(userId, qhdIds);
		addMessage(redirectAttributes, "取货分派单成功");
		return "redirect:" + Global.getAdminPath() + "/order/quHuoDan/?repage";
	}
	

}
